<?

JCAT_Package::ImportClass('JCAT_GtkDialog',JCAT::PACKAGE_MVCGTK) ;

class CoderDlgBase extends JCAT_GtkDialog
{
	/**
	 * what's this
	 * 
	 * @access	public
	 * @param	$aParent=null	JCAT_ViewGtk		拥有此视图的对象
	 * @param	$aUI=null		JCAT_UIGtkGlade		模版引擎
	 * @return	array
	 */
	public function CoderDlgBase( JCAT_IViewContainer $aParent, JCAT_UIGtkGlade $aUI=null )
	{
		$this->JCAT_GtkDialog('CoderDlgBase','CoderDlgBase','DlgCoderBase',$aParent,$aUI) ;
		
		$this->AddUICtrl( JCAT_Package::ImportInstance(dirname(__FILE__).'/instance.CloseAfterSave.php') );
		
		
		// 子视图
		$aSubView = new DlgHeadView($this) ;
		$this->AddView($aSubView) ;
	}
	
	
	
	/**
	 * 响应 MainMenuOpen 菜单项
	 * 
	 * @access	public
	 * @return	void
	 */
	public function OnMainMenuOpenActivate()
	{
		
	}
	
	/**
	 * what's this
	 * 
	 * @access	public
	 * @return	void
	 */
	public function OnOk()
	{
		$aController = $this->GetParentController() ;
		$aController = $aController->GetTopController() ;
		
		JCAT_ASSERT::ASSERT_INSTANCE($aController,'IToolboxCoder');
		
		if( !$aController->Save() )
		{
			return ;
		}
		
		else 
		{
			// 取消 标题栏 上 的 * 号
			$sTitle = $this->GetTitle() ;
			$sTitle = preg_replace('/^\* /','',$sTitle) ;
			$this->SetTitle($sTitle) ;
		
			// 关闭对话框
			if( $this->GetUICtrlExchangeData('CloseAfterSave') )
			{
				parent::OnOk() ;
			}
		}
	}
	
	
	/**
	 * what's this
	 * 
	 * @access	public
	 * @return	void
	 */
	public function OnCancel()
	{
		// 取得文档
		$aCoderDoc = $this->GetDocument() ;
		JCAT_ASSERT::ASSERT_INSTANCE($aCoderDoc,'CoderDocBase') ;
		
		// 主视图
		$aMainView = JCAT_View::GetViewByGlobalName('MainView') ;
		JCAT_ASSERT::ASSERT_INSTANCE($aMainView) ;
		
		// 已经改变
		if( $aCoderDoc->IsChanged() or $aMainView->IsChanged() )
		{
			$aDlg = new JCAT_GtkDialog('DlgQuerySave','CoderDlgBase','DlgQuerySave',$this) ;
			
			// 创建窗口
			$aDlg->Create() ;
			
			// 路径
			$wndLabelPath = $aDlg->GetChildWidget('LabPath') ;
			JCAT_ASSERT::ASSERT_INSTANCE($wndLabelPath,'GtkLabel') ;
			$wndLabelPath->set_text($aCoderDoc->GetPath()) ;
			
			// 执行对话框
			$nResponse = $aDlg->DoDialog() ;
			
			switch($nResponse)
			{
				// 取消
				case 1 :
					return ;
				
				// 保存
				case 2 :
					
					$aController = $this->GetParentController() ;
					$aController = $aController->GetTopController() ;
					
					JCAT_ASSERT::ASSERT_INSTANCE($aController,'IToolboxCoder');
					$aController->Save() ;
					
					break ;
				
				// 丢弃
				case 3 :
					// 什么也不做 ... ...
					break ;
				
				// 异常值
				default :
					JCAT_ASSERT::ASSERT_(false,'!?') ;
					break ;
			}
		}

		parent::OnCancel() ;
	}
	
	
	/**
	 * what's this
	 * 
	 * @access	public
	 * @return	void
	 */
	public function OnNew()
	{
		$aController = $this->GetParentController() ;
		$aController = $aController->GetTopController() ;
		
		JCAT_ASSERT::ASSERT_INSTANCE($aController,'IToolboxCoder');
		
		$aController->CreateNew() ;
	}

	
	/**
	 * what's this
	 * 
	 * @access	public
	 * @return	void
	 */
	public function OnDialogClose()
	{
		$this->OnCancel() ;
		
		return true ;
	}
	
	/**
	 * what's this
	 * 
	 * @access	public
	 * @return	bool
	 */
	public function QuerySave()
	{		
	}

	public function OnUpdate()
	{		
		$aDocument = $this->GetDocument() ;
		
		// 在标题中显示 星号
		if( $aDocument and $aDocument->IsChanged() )
		{
			$wndWidget = $this->GetWidget() ;
			if($wndWidget)
			{
				$sTitle = $wndWidget->get_title() ;
				if( substr($sTitle,0,2)!='* ' )
				{
					$wndWidget->set_title('* '.$sTitle) ;
				}
			}
		}
	}
}

?>